package com.ziiwee.reduce.rest.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

@Component
public class SpringUtil implements ApplicationContextAware {

    private static ApplicationContext CONTEXT = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        CONTEXT = applicationContext;
    }

    public static ApplicationContext getContext() {
        Assert.notNull(CONTEXT, "ApplicationContext 为 null");
        return CONTEXT;
    }

    public static <T> T getBean(Class<T> type) {
        return getContext().getBean(type);
    }

    public static Object getBean(String name) {
        return getContext().getBean(name);
    }

    public static <T> T getBean(String name, Class<T> type) {
        return getContext().getBean(name, type);
    }

}
